<?php
/**
 * div ที่จำเป็นต้องมี ดังนี้
 * div[id=divFirstHeader] สำหรับแสดงที่หัวหน้าแรก
 * div[class=header] ส่วนหัวที่จะปรากฏทุกหน้า
 *
 * div[main] ส่วนตารางข้อมูลที่จะแสดง
 *
 * div[main] table thead หัวตารางข้อมูล
 * div[main] table tbody รายการข้อมูล
 *
 * div[class=footer] ส่วนท้ายของแต่ละหน้า
 * div[id=divLastFooter] สำหรับแสดงที่หน้าสุดท้าย
 *
 * ******* จำเป็นมาก แม้ไม่มีข้อมูลในส่วนนั้นก็ต้องมี div เหล่านี้อยู่ เพื่อให้การแบ่งหน้าทำได้ถูกต้อง ************
 */
?>
<div id="divFirstHeader">
    <div class="title">
        <?php echo $this->element('print/title'); ?>
    </div>
    <div class="documentTitle">
        <div class="documentNote">
            VMI REPORT: Customer Dispatch
        </div>
        <table>
            <tr>
                <td><label>Report date</label></td>
                <td><?php echo $report_date; ?></td>
                <td><label>Report by</label></td>
                <td><?php echo $user; ?></td>
                <td><label>Page</label></td>
                <td><span class="pageNo">1/1</span></td>
            </tr>
            <?php if (!empty($criteria['Product']['part_no'])) : ?>
            <tr>
                <td width="90px"><label>Stock Code</label></td>
                <td colspan="3"><?php echo $criteria['Product']['part_no']; ?></td>
            </tr>
            <?php endif; ?>
            <?php if (!empty($criteria['Dispatch']['customer_id'])) : ?>
            <tr>
                <td width="90px"><label>Customer</label></td>
                <td colspan="3"><?php echo $customer_name; ?></td>
            </tr>
            <?php endif; ?>
            <?php if (!empty($criteria['Dispatch']['site_id'])) : ?>
            <tr>
                <td width="90px"><label>Site</label></td>
                <td colspan="3"><?php echo $site_name; ?></td>
            </tr>
            <?php endif; ?>
            <?php if (!empty($criteria['Product']['category_id'])) : ?>
            <tr>
                <td width="90px"><label>Category</label></td>
                <td colspan="3"><?php echo $category_name; ?></td>
            </tr>
            <?php endif; ?>
            <?php if (!empty($criteria['Product']['supplier_id'])) : ?>
            <tr>
                <td width="90px"><label>Vendor</label></td>
                <td colspan="3"><?php echo $vendor_name; ?></td>
            </tr>
            <?php endif; ?>
            <?php if (!empty($criteria['Product']['brand_id'])) : ?>
            <tr>
                <td width="90px"><label>Brand</label></td>
                <td colspan="3"><?php echo $brand_name; ?></td>
            </tr>
            <?php endif; ?>
            <?php if (!empty($criteria['Dispatch']['dispatch_date_start'])) : ?>
            <tr>
                <td width="90px"><label>Dispatch date</label></td>
                <td colspan="3">From <?php echo $criteria['Dispatch']['dispatch_date_start']; ?> to <?php echo $criteria['Dispatch']['dispatch_date_end']; ?></td>
            </tr>
            <?php endif; ?>
            <?php if (!empty($criteria['Option']['order1'])) : ?>
            <tr>
                <td width="90px"><label>Order by</label></td>
                <td colspan="3">
                        <?php echo !empty($criteria['Option']['order1'])? $orderOptions[$criteria['Option']['order1']]: ''; ?>
                        <?php echo !empty($criteria['Option']['order2'])? $orderOptions[$criteria['Option']['order2']]: ''; ?>
                        <?php echo !empty($criteria['Option']['seq']) && $criteria['Option']['seq'] == 'desc'? ' Descendent': ' Ascendent'; ?>
                </td>
            </tr>
            <?php endif; ?>
            <?php if (!empty($criteria['Option']['limit'])) : ?>
            <tr>
                <td width="90px"><label>Show</label></td>
                <td><?php echo $criteria['Option']['limit']; ?> entries</td>
            </tr>
            <?php endif; ?>
        </table>
    </div>
</div>

<!--div class="header">
</div-->

<div class="main">
    <table id="dataTable" class="dataTable" cellspacing="0" cellpadding="0">
        <thead>
            <tr>
                <th class="no">No.</th>
                <th>Part no.<?php if ($display_dispatch_no == true) echo '/ <br /> Dispatch no.'; ?></th>
                <th>Stock code <?php if ($display_dispatch_no == true) echo '/ <br /> Dispatch Date'; ?></th>
                <th>Customer no.</th>
                <th>Customer Name  <?php if ($display_dispatch_no == true) echo '/ <br /> Product Description'; ?></th>
                <th>Site no.</th>
                <th>Site Name</th>
                <th width="40px">Freq.</th>
                <th width="45px">Price</th>
                <th width="40px">Qty.*</th>
                <th width="50px">Amount</th>
            </tr>
        </thead>
        <tbody>
            <?php $i = 0;
            $r = 0;
            $total_freq = 0;
            $total_qty = 0;
            $total_amount = 0;
            foreach($this->data as $i => $data): ?>
            <tr>
                <td class="center"><?php echo ($i + 1); ?></td>
                <td class="<?php echo $display_dispatch_no == true? 'bold': ''; ?>">
                        <?php echo $data['Product']['part_no']; ?>
                </td>
                <td class="<?php echo $display_dispatch_no == true? 'bold': ''; ?>">
                        <?php echo $data['CustProductSite']['stock_code']; ?>
                </td>
                <td class="<?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo $data['Customer']['cust_no']; ?></td>
                <td class="<?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo $data['Customer']['name']; ?></td>
                <td class="<?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo $data['Site']['site_no']; ?></td>
                <td class="<?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo $data['Site']['name']; ?></td>
                <td class="text-right <?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo $data[0]['frequency']; ?></td>
                <td class="text-right <?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo number_format($data[0]['price'], 2); ?></td>
                <td class="text-right <?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo $data[0]['stock_in_qty']; ?></td>
                <td class="text-right <?php echo $display_dispatch_no == true? 'bold': ''; ?>"><?php echo number_format($data[0]['price'] * $data[0]['stock_in_qty'], 2); ?></td>
                    <?php
                    $total_freq += $data[0]['frequency'];
                    $total_qty += $data[0]['stock_in_qty'];
                    $total_amount += $data[0]['price'] * $data[0]['stock_in_qty'];
                    ?>
            </tr>
                <?php if ($display_dispatch_no == true) :
                    $stos = $this->requestAction('its_reports/get_dispatch_no/' . $data['Product']['id'] . '/' . $data['CustProductSite']['id']);
                    // ตัวแปรสำหรับลำดับเลข stock out no
                    $k = 1;
                    foreach($stos as $sto) :
                        ?>
            <tr>
                <td class="rowTd">&nbsp;</td>
                            <?php if ($display_dispatch_no == true) : ?>
                <td>
                    <span><?php echo $k; ?>.</span>&nbsp;
                    <span><?php echo $sto['Dispatch']['dispatch_no']; ?></span>
                </td>
                <td><?php echo $sto[0]['dispatch_date']; ?></td>
                <td>&nbsp;</td>
                <td><?php echo $k == 1? $data['Product']['text']: '&nbsp;'; ?></td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td class="text-right"><?php echo $sto['CustStockIn']['stock_in_qty']; ?></td>
                <td class="text-right"><?php echo number_format($sto['CustStockIn']['stock_in_qty'] * $data[0]['price'], 2); ?></td>
                            <?php endif; ?>
            </tr>
                        <?php ++$k;
                    endforeach; ?>
                <?php endif; ?>
            <?php
            endforeach; ?>
            <tr class="trFooter">
                <td colspan="7" class="bold text-right">Grand Total</td>
                <td class="text-right bold"><?php echo number_format($total_freq); ?></td>
                <td class="text-right">-</td>
                <td class="text-right bold"><?php echo number_format($total_qty); ?></td>
                <td class="text-right bold"><?php echo number_format($total_amount, 2); ?></td>
            </tr>
        </tbody>
    </table>
</div>

<div class="footer"></div>

<div id="divLastFooter">
    <span class="caption">* Only Products that have been stocked in by Customer are counted.</span>
</div>